package com.czk.netty.netty.tcp.protocaltcp.server;

import com.czk.netty.netty.tcp.protocaltcp.protocol.MessageProtocol;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;

import java.util.List;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/1/24 17:00
 */
public class MyMessageDecoder extends ReplayingDecoder<Void> {
    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
        System.out.println("MyMessageDecoder decode方法被调用了");
        // 将字节转换成messageProtocol
        int len = byteBuf.readInt();
        byte[] content=new byte[len];
        byteBuf.readBytes(content);
        // 封装成messageProtocol对象，传给下一个handler处理
        MessageProtocol messageProtocol=new MessageProtocol();
        messageProtocol.setLen(len);
        messageProtocol.setContent(content);
        list.add(messageProtocol);
    }
}
